/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package models;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import tableModels.TableLieuAVisiterModel;
import utils.Connexion;

/**
 *
 * @author Betty
 */
public class LieuAVisiterMapper {
    private Connexion _connexion;
    
    public LieuAVisiterMapper() {
        try {
            this._connexion = new Connexion();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ClientMapper.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ClientMapper.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public TableLieuAVisiterModel fetchAll() {
        String requete = "SELECT ID, NOMLIEU, VILLE, PAYS, DESCRITIF, PRIXVISITE FROM PROJET.LIEUAVISITER";
        String[] titre = {"IDENTIFIANT","LIEU","VILLE","PAYS","DESCRIPTIF", "PRIX DE LA VISITE"};
        TableLieuAVisiterModel  model = null;
        try {
            model = new TableLieuAVisiterModel(this._connexion,requete,titre);
            model.getRowCount();
        } catch (SQLException ex) {
            Logger.getLogger(ClientMapper.class.getName()).log(Level.SEVERE, null, ex);
        }
            return model;
    }
    
    private void update(LieuAVisiter lieuAVisiter) {
        try {
            String requete = "UPDATE LIEUAVISITER.ETAPE SET NOMLIEU='" + lieuAVisiter.getNomLieu() + "',"
                    + "VILLE='" + lieuAVisiter.getVille() +"', PAYS='" + lieuAVisiter.getPays() +"',"
                    + "DESCRITIF='" + lieuAVisiter.getDescriptif() + "',PRIXVISITE='" + lieuAVisiter.getPrixVisite() + "'"
                    + " WHERE ID=" + lieuAVisiter.getIdentifiant();
            this._connexion.setUpdate(requete);
        } catch (SQLException ex) {
            Logger.getLogger(LieuAVisiterMapper.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public LieuAVisiter find(int id, LieuAVisiter lieuAVisiter) throws SQLException  {
        String requete = "SELECT ID, NOMLIEU, VILLE, PAYS, DESCRITIF, PRIXVISITE FROM FROM PROJET.LIEUAVISITER WHERE ID=" + id;
        this._connexion.setQuery(requete);
        ResultSet resultat = this._connexion.getResultSet();
        while(resultat.next()){
            lieuAVisiter.setIdentifiant(Integer.parseInt(resultat.getString("IDENTIFIANT")));
            lieuAVisiter.setNomLieu(resultat.getString("NOMLIEU"));
            lieuAVisiter.setVille(resultat.getString("VILLE"));
            lieuAVisiter.setPays(resultat.getString("PAYS"));
            lieuAVisiter.setDescriptif(resultat.getString("DESCRITIF"));
            lieuAVisiter.setPrixVisite(Integer.parseInt(resultat.getString("PRIXVISITE")));
            
        }
        return lieuAVisiter;
    }
    
    public void save(LieuAVisiter lieuAVisiter) {
        if(lieuAVisiter.getIdentifiant()== 0){
            this.add(lieuAVisiter);
        } else {
           this.update(lieuAVisiter);
        }
    }
    
    private void add(LieuAVisiter lieuAVisiter) {
        try {
            String requete = "INSERT INTO LIEUAVISITER.ETAPE VALUES(0,'" + lieuAVisiter.getNomLieu() + "',"
                    + "'" + lieuAVisiter.getVille() + "','" + lieuAVisiter.getPays() +"',"
                    + "'" + lieuAVisiter.getDescriptif() + "','" + lieuAVisiter.getPrixVisite() + "')";
            this._connexion.setUpdate(requete);
        } catch (SQLException ex) {
            Logger.getLogger(LieuAVisiterMapper.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
